package com.hxgis.electronstarter.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

/**
 * @author zhaoheng
 */
@Configuration
public class QyCorsConfig {
    @Bean
    public CorsFilter corsFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        // 1 设置访问源地址    //允许所有域名进行跨域调用  也可以单独设置前端ip地址
        corsConfiguration.addAllowedOrigin("*");
        // 2 设置访问源请求头   //放行全部原始头信息
        corsConfiguration.addAllowedHeader("*");
        // 3 设置访问源请求方法    //允许所有请求方法跨域调用
        corsConfiguration.addAllowedMethod("*");
        // 4 对接口配置跨域设置
        source.registerCorsConfiguration("/**", corsConfiguration);
        return new CorsFilter(source);
    }
}